$(function (){

    $.ajax({
        url:"/api/teacher/list"
    }).done(function (data)  {
        //console.log(data)
        let html="";
        data.forEach((element ,index)=>{
            html+="<tr>"
            html+="<td>"+element.id+"</td>"
            html+="<td>"+element.name+"</td>"
            html+="<td>"+element.sno+"</td>"
            html+="<td>"+element.college+"</td>"

            html+="</tr>"
        })
        $("#teacherTh").html(html)
    });

    });
$(function (){
    loadTeacherList();
});

function showTeacherDlg(id){

    let title="新增教师"
    if(id){

        title="编辑教师"
        $("#formId").css("display","block");
        $.ajax({
            url:"/api/teacher/"+id,
            method: "GET"
        }).done(result=>{
            console.log(result)
            $.each(result,function (key,value){
                var field=$('#teacherForm [name="'+key+'"]');
                if(field.is(':radio')){
                    field.filter('[value="'+value+'"]').prop('checked',true);
                }else if(field.is(':checkbox')){
                    field.prop('checked',value==="yes");
                }else{
                    field.val(value);
                }
            });
        })

    }else{
        $("#teacherForm")[0].reset();
        $("#formId").css("display","none");
    }
    layerIndex=layer.open(
        {
            type:1,

            area:['520px','auto'],
            content:$('#teacherForm')//捕获层
        });
}
layui.use(function (){
    layui.form.on('submit(stud-dlg)',function (data){
        event.preventDefault();
        commitStuDlg();
    })
})
function loadTeacherList (){

    $.ajax({
        url:"/api/teacher/list"
    }).done(function (data)  {
        //console.log(data)
        let html="";
        data.forEach((element ,index)=>{
            html+="<tr>"
            html+="<td>"+element.id+"</td>"
            html+="<td>"+element.name+"</td>"
            html+="<td>"+element.sno+"</td>"
            html+="<td>"+element.college+"</td>"
            html += "<td> <a href='#' onclick='showTeacherDlg(" + element.id + ")'>编辑</a>&nbsp;&nbsp;<a href='#' onclick='deleteById("+element.id+")'>删除</a></td>"
            html+="</tr>"
        })
        $("#teacherTh").html(html)
    });

}
let layerIndex;
/**
 * 弹出对话框
 */


function commitStuDlg(){

    let id=$("#id").val();
    let formData = $("#teacherForm").serialize();
    if(id!=null&&id!=""){
        $.ajax(
            {
                url: "/api/teacher/update",
                method: "PUT",
                data: formData

            }).done(result => {
            console.log(result);
            if (result.id) {

                loadTeacherList();

                console.log("add success")
                if (layerIndex)
                    layer.close(layerIndex)

            }
        }).fail((jqXHR, textStatus, errorThrown) => {
            console.error("Request failed" + textStatus + "-" + errorThrown);
            alert("error,try");
        });
    }else {

        let formData = $("#teacherForm").serialize();

        $.ajax(
            {
                url: "/api/teacher/add",
                method: "POST",
                data: formData

            }).done(result => {
            console.log(result);
            if (result.id) {

                loadTeacherList();

                console.log("add success")
                if (layerIndex)
                    layer.close(layerIndex)

            }
        }).fail((jqXHR, textStatus, errorThrown) => {
            console.error("Request failed" + textStatus + "-" + errorThrown);
            alert("error,try");
        });
    }
    $("#btnOK").prop("disabled",true).addClass("layui-btn-disabled");

}
function deleteById(id){
    layer.confirm('真的要删除吗？',{icon:3},function (){
       $.ajax({
           url:"/api/teacher/delete/"+id,
           method:"DELETE"
       }).done(result=>{
           loadTeacherList();
       })
        layer.closeAll();
    },function (){
    });

}